package com.cqcvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqcvc.entity.RolePermission;
import com.cqcvc.mapper.RolePermissionMapper;
import com.cqcvc.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hunter
 * @since 2022-08-04
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public List<RolePermission> selectByRoleId(Integer rid) {
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("role_id",rid);
        return rolePermissionMapper.selectList(qw);
    }

    @Override
    public void saveRolePermission(Integer rid, Integer[] permId) {
        //先删除rid在role_permission表中的对应记录
        QueryWrapper qw=new QueryWrapper();
        qw.eq("role_id",rid);
        rolePermissionMapper.delete(qw);
        //将rid和permId信息写入role_permission表中
        for(Integer pid:permId){
            RolePermission rolePermission=new RolePermission(rid,pid);
            rolePermissionMapper.insert(rolePermission);
        }

    }
}
